
import Admin.Admin;
import Student.ForStudent;
import Teacher.ForTeacher;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

/*
 * Created by JFormDesigner on Tue Aug 06 15:04:08 CST 2024
 */



/**
 * @author 29454
 */
public class login extends JFrame {
    public login() {
        initComponents();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(650,225,400,400);
    }

//    private void login(ActionEvent e) {
//        // TODO add your code here
//
//    }

public void login(ActionEvent e) {
    if (e.getSource() == button1) {
        // 获取用户输入的账户和密码
        String username = textField1.getText();
        String password = new String(passwordField1.getPassword());
        int role=validateUser(username, password);

        // 连接到数据库并验证用户
        if (role==0) {
            JOptionPane.showMessageDialog(this, "登录成功！");
            this.dispose();
            Admin admin = new Admin();
            admin.setVisible(true);
        }
        else if (role==2) {
            JOptionPane.showMessageDialog(this, "登录成功！");
            this.dispose();
            ForStudent student =new ForStudent(Integer.valueOf(username));
            student.setVisible(true);
        }
        else if (role==1) {
            JOptionPane.showMessageDialog(this, "登录成功！");
            this.dispose();
            ForTeacher teacher =new ForTeacher(Integer.valueOf(username));
            teacher.setVisible(true);
        }
        else {
            JOptionPane.showMessageDialog(this, "登录失败，请检查用户名和密码！");
        }
    }
}

    private int validateUser(String username, String password) {
        try {
            // 连接到 MySQL 数据库
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/educationalmanagementsystem", "root", "Atlantis0130");

            // 查询用户表
            String query = "SELECT * FROM userlogin WHERE userName = ? AND password = ?";
            try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
                preparedStatement.setString(1, username);
                preparedStatement.setString(2, password);

                // 执行查询
                ResultSet resultSet = preparedStatement.executeQuery();
                if(resultSet.next()) {
                    int fieldValue = resultSet.getInt("role"); // 用字段名获取字符串值
                    return fieldValue;
                }
                else return 3;

                // 如果有匹配的记录，则验证通过

            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "数据库连接或查询出现问题：" + ex.getMessage());
            return 3;
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        label3 = new JLabel();
        passwordField1 = new JPasswordField();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8d26\u6237");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(95, 115), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(95, 165), label2.getPreferredSize()));
        contentPane.add(textField1);
        textField1.setBounds(140, 115, 140, textField1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u767b\u9646");
        button1.addActionListener(e -> login(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(155, 225), button1.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u767b\u9646\u754c\u9762");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(170, 65), label3.getPreferredSize()));
        contentPane.add(passwordField1);
        passwordField1.setBounds(140, 165, 140, passwordField1.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JButton button1;
    private JLabel label3;
    private JPasswordField passwordField1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
